﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CPPEI.Coolzon.UI.Design
{
    public partial class DesignData
    {
        /// <summary>
        /// 设置组件大小
        /// 0, 1, 2,
        /// 3, 4, 5,
        /// 6, 7, 8,
        /// </summary>
        /// <param name="resizeDirection"></param>
        /// <param name="curPointInCanvas">point in scene</param>
        public void ResizePart(int resizeDirection, Point curPointInCanvas, Point offset)
        {
            //PartContainer container = this._selectedPartIntance.ParentPart as PartContainer;
            //UIElement containerContent = container.GetContentContainer();
            //Point ptInPartContainerContent = PointFromSceneToElement(containerContent, curPointInCanvas);

            double deltaWidth = this.ResizeBeginPointInCanvas.Value.X - curPointInCanvas.X;
            double deltaHeight = this.ResizeBeginPointInCanvas.Value.Y - curPointInCanvas.Y;
            if (resizeDirection == 0)
            {
                #region // 左上
                double width = this.ResizeBeginPartSize.Value.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }
                double height = this.ResizeBeginPartSize.Value.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double left = this.ResizeBeginPartPos.Value.X - deltaWidth;
                double top = this.ResizeBeginPartPos.Value.Y - deltaHeight;

                this._selectedPartIntance.XLeft = left;
                this._selectedPartIntance.XTop = top;

                this._selectedPartIntance.XWidth = width;
                this._selectedPartIntance.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 1)
            {
                #region // 上
                double height = this.ResizeBeginPartSize.Value.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double top = this.ResizeBeginPartPos.Value.Y - deltaHeight;

                this._selectedPartIntance.XTop = top;
                this._selectedPartIntance.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 2)
            {
                #region 右上
                double height = this.ResizeBeginPartSize.Value.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }
                double width = this.ResizeBeginPartSize.Value.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double top = this.ResizeBeginPartPos.Value.Y - deltaHeight;

                this._selectedPartIntance.XWidth = width;
                this._selectedPartIntance.XHeight = height;
                this._selectedPartIntance.XTop = top;
                #endregion
            }
            else if (resizeDirection == 3)
            {
                #region // 左
                double width = this.ResizeBeginPartSize.Value.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double left = this.ResizeBeginPartPos.Value.X - deltaWidth;

                this._selectedPartIntance.XLeft = left;
                this._selectedPartIntance.XWidth = width;
                #endregion
            }
            else if (resizeDirection == 4)
            {
                // 移动组件的位置
                double left = this.ResizeBeginPartPos.Value.X - deltaWidth;
                double top = this.ResizeBeginPartPos.Value.Y - deltaHeight;

                this._selectedPartIntance.XLeft = left;
                this._selectedPartIntance.XTop = top;
            }
            else if (resizeDirection == 5)
            {
                #region 右
                double width = this.ResizeBeginPartSize.Value.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }
                this._selectedPartIntance.XWidth = width;
                #endregion
            }
            else if (resizeDirection == 6)
            {
                #region 左下
                double width = this.ResizeBeginPartSize.Value.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double height = this.ResizeBeginPartSize.Value.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double left = this.ResizeBeginPartPos.Value.X - deltaWidth;

                this._selectedPartIntance.XLeft = left;
                this._selectedPartIntance.XWidth = width;
                this._selectedPartIntance.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 7)
            {
                // 下
                double height = this.ResizeBeginPartSize.Value.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }
                this._selectedPartIntance.XHeight = height;
            }
            else if (resizeDirection == 8)
            {
                double width = this.ResizeBeginPartSize.Value.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double height = this.ResizeBeginPartSize.Value.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                this._selectedPartIntance.XWidth = width;
                this._selectedPartIntance.XHeight = height;
            }

            Size borderSize = new Size(this._selectedPartIntance.XWidth, this._selectedPartIntance.XHeight);
            this._ctlDesignBorder.SetSize(borderSize);

            Point borderOffset = PointElementPartToCanvas(this._selectedPartIntance, new Point(0, 0));
            _ctlDesignBorder.SetOffset(borderOffset);
        }
    }
}
